# ------------------------------------------------------------------------------
# Gets scores from lasso
# Author: Cassidy Shubatt <cshubatt@gmail.com>
# To run: bsub -q medium -R "rusage[mem=1000]" bash 04_predict_unpenalized_along_path.sh
# ------------------------------------------------------------------------------

# Seeding ----------------------------------------------------------------------
set.seed(1)

# Libraries --------------------------------------------------------------------
message("Loading libraries...")
library(here)
library(yaml)
library(tidyverse)
library(glue)
library(Matrix)
library(fastglm)

temp <- here(
  "code", "06_physician_boundedness", "01_behavioral_lasso", "temp"
)
# Load Data --------------------------------------------------------------------
message("Loading data...")
split <- "random"
paths <- read_yaml(here("lib", "filepaths.yml"))
overnight_lab <- ""
x <- readRDS(glue(paths$features$test)) %>%
  as.matrix %>%
  as.data.frame %>%
  mutate(Constant = 1)
glms <- readRDS(file.path(temp, "unpenalized_glm__stent_or_cabg_010_day.rds"))

# Predict ----------------------------------------------------------------------
message("Predicting")
predictions <- transmute(glms,
  n_coef = map_int(which_cols, length),
  order_name = "stent_or_cabg_010_day",
  model_name = "glm",
  score_name = "stent_or_cabg_010_day",
  score = map2(
    fit, which_cols,
    ~ predict(.x, as.data.frame(x[, c(.y, ncol(x))]), type = "response")
  )
)

# Save -------------------------------------------------------------------------
message("Saving...")
write_rds(predictions, file.path(temp, "scores__glm__stent_or_cabg_010_day.rds"))

# ------------------------------------------------------------------------------
message("Done.")
